use dta/SMDLevel.dta, clear

gen SECOND=0
replace SECOND=1 if votes2_TOTAL!=0  

*********************** party running **********************************************
preserve
foreach party in s v_a v a l t kir h_fv h fv oth nsa nkp {
gen runF_`party'=run1_`party'
replace runF_`party'=run2_`party' if SECOND==1
}
keep year SMD run*
sort year SMD
save dta/SMDLevel_Run, replace
restore


*********************** CREATE BLOCS **********************************************
foreach round in 1 2 {
gen vs`round'_LAB=vs`round'_s+vs`round'_nsa+vs`round'_nkp 
gen vs`round'_LIB=vs`round'_a+vs`round'_v_a+vs`round'_v 
gen vs`round'_CON=vs`round'_fv+vs`round'_h_fv+vs`round'_h 
gen vs`round'_OTH=1-vs`round'_LAB-vs`round'_LIB-vs`round'_CON
}

foreach bloc in LAB LIB CON OTH {
gen vsF_`bloc'=vs1_`bloc'
replace vsF_`bloc'=vs2_`bloc' if SECOND==1
}

*** generating leads and lags
xtset SMD year

foreach round in 1 2 F {
	foreach bloc in LAB LIB CON OTH {
	gen next_vs`round'_`bloc'=f3.vs`round'_`bloc'
	gen prev_vs`round'_`bloc'=l3.vs`round'_`bloc'
	}
}

keep year SMD *vs*LAB* *vs*LIB* *vs*CON* *vs*OTH* PR 
order year SMD vs1* vs2* vsF* next_vs1* next_vs2* next_vsF* prev_vs1* prev_vs2* prev_vsF* PR



********************************************************
reshape long vs1_ vs2_ vsF_ next_vs1_ next_vs2_ next_vsF_ prev_vs1_ prev_vs2_ prev_vsF_, i(SMD year) string
rename _j bloc
order year SMD 
********************************************************
gen reform=0
replace reform=1 if year>1920


*********************** merge: party running **********************************************
sort year SMD
merge year SMD using dta/SMDLevel_Run
assert _merge==3
drop _merge


***************************************************************************
***************** all three blocs running in all election years************
***************************************************************************

foreach round in 1 2 F{
gen run`round'_LAB=run`round'_s+run`round'_nsa+run`round'_nkp>0
gen run`round'_LIB=run`round'_a+run`round'_v_a+run`round'_v >0
gen run`round'_CON=run`round'_fv+run`round'_h_fv+run`round'_h >0
}

foreach round in 1 2 F{
gen all`round'=0
replace all`round'=1 if (run`round'_LAB+run`round'_LIB+run`round'_CON==3)
}

bysort year: tab1 all*
egen sum_all1=sum(all1),by(SMD bloc)
egen sum_allF=sum(allF),by(SMD bloc)

***************************************************************************
***************** September 2018 : analysis of why SMDs drop out **********
***************************************************************************
egen sum_all1_reform=sum(all1),by(SMD bloc reform)

tab sum_all1_reform if year==1918 & bloc=="LAB"  /* 43 SMDs where all blocs run in all four pre-reform years */
tab sum_all1_reform if year==1921 & bloc=="LAB"  /* 86 SMDs where all blocs run in all three post-reform years */
sum run1_LAB run1_LIB run1_CON if year==1921 

***************************************************************************
***************************************************************************

keep if sum_all1==7 

gen sample=1
collapse sample, by(SMD)
save dta/StableSample.dta, replace
